
import { defineStore } from 'pinia';
import { postDeptList, postJobList } from '@/api/system';
import { postRoleList } from '@/api/common';
const commonEnumApi = {
    role: {
        api: postRoleList,
        replaceField: {
            label: 'roleName',
            value: 'id'
        }
    },
    department: {
        api: postDeptList,
        replaceField: {
            label: 'name',
            value: 'id'
        }
    },
    job: {
        api: postJobList,
        replaceField: {
            label: 'name',
            value: 'id'
        }
    }
}
export const useStoreCommonEnum = defineStore('commonEnum', {
    state: () => {
        return {
            commonEnum: {

            }
        };
    },
    actions: {
        async getCommonEnum(key, needUpdate = false) {
            console.log('key', key);
            const { api, replaceField } = commonEnumApi[key]
            if (!this.commonEnum[key] || needUpdate) {
                const res = await api()
                this.commonEnum[key] = res.data.map(item => {
                    let obj = {}
                    for (let key in replaceField) {
                        obj[key] = item[replaceField[key]]
                    }
                    return {
                        ...item,
                        ...obj
                    }
                })
                // this.commonEnum[key] = [{
                //     label: '12',
                //     value: 11
                // }]
            }
            return Promise.resolve(this.commonEnum[key])

        },
    },
    //   persist: {
    //     key: 'store-user',
    //     storage: window.localStorage,
    //     paths: ['userInfo', 'token', 'rsaKey', 'activateUser', 'searchHistory'],
    //   },
});
